#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Copyright © 启明星辰 版权所有
# @Time    : 2024/5/29 18:16
# @Author  : 胡浩浩
# @File    : url_path.py
# @IDE     : PyCharm
# @description :
import hashlib
import json

# from urllib.parse import urlparse, parse_qs
#
# url = "https://192.168.114.89:8443/v3/firefox/global-recs?version=3&consumer_key=&locale_lang=zh-CN&region=CN&count=30&count=40"
#
# parsed_url = urlparse(url)
# query_params = parse_qs(parsed_url.query)
#
# print("Scheme:", parsed_url.scheme)
# print("Netloc:", parsed_url.netloc)
# print("Path:", parsed_url.path)
# print("Params:", parsed_url.params)
# print("Query:", query_params)
# print("Fragment:", parsed_url.fragment)
import json
import time
from hashlib import md5


def dic_to_json(dic):
    return json.dumps(dic, ensure_ascii=False)


def get_random_int(number_list: list) -> int:
    import random
    return random.choice(number_list)


function_list = [
    ["test_case",
     """def test_case():
    return 1==2
    """
     ],
    ["dic_to_json",
     """def dic_to_json(dic):
        import json
        return json.dumps(dic, ensure_ascii=False)
     """,
     {"a": 1, "b": 2}
     ],
    ["get_random_int",
     """def get_random_int(number_list:list)->int:
        import random
        return random.choice(number_list)
    """,
     [1, 2, 3, 4, 5]
     ]

]


# class FunctionExecutor:
#     def __init__(self, function_list):
#         self.function_list = function_list
#
#     def execute_functions(self):
#         results = []
#         for item in self.function_list:
#             exec(item[1])
#             result = eval(f"{item[0]}({item[2]})")
#             results.append(result)
#         return results
#
# if __name__ == '__main__':
#     FunctionExecutor(function_list)


class FunctionExecutor:
    def __init__(self, function_name, function_code, params=None):
        self.function_name = function_name
        self.function_code = function_code
        self.params = params

    def execute_function(self):
        exec(self.function_code)
        if self.params is None:
            result = eval(f'{self.function_name}()')
        else:
            result = eval(f'{self.function_name}({self.params})')
        print(result)
        return result



if __name__ == '__main__':
    data_list=[
        "print_item",
        """def print_item(item):
    print(item)
    return item
        """,
        [{'a':'a'}]

    ]
    FunctionExecutor(*data_list).execute_function()
    # ts=str(int(time.time()*1000))
    # md51=md5("64234aeb5392f125c811513e3b702ca7".encode()).hexdigest()
    # sign="sysadmin:"+md5(f"{ts}/api/v1/host/infosysadmin{md51}".encode()).hexdigest()
    # print(ts)
    # print(sign)
    # ts = int(time.time() * 1000)
    # local_secret = "sysadmin:" + hashlib.md5((str(ts) + "/api/v1/host/info" + "sysadmin" + hashlib.md5("64234aeb5392f125c811513e3b702ca7".encode())).encode()).hexdigest()
    # print(ts)
    # print(local_secret)
    import time
    import hashlib


    def main():
        ts = int(time.time() * 1000)
        local_secret = "sysadmin:" + hashlib.md5((str(ts) + "/api/v1/host/info" + "sysadmin" + hashlib.md5(
            "64234aeb5392f125c811513e3b702ca7".encode()).hexdigest()).encode()).hexdigest()
        print(ts)
        print(local_secret)


    if __name__ == "__main__":
        main()